const Modem = require('../lib/modem');

let opt = {
  dataBits: 8,
  stopBits: 1,
  parity: 'none', //校验
  baudRate: 9600,
  // rtscts: true,
  autoOpen: true,
};

let modem = new Modem('COM9', opt);

modem.on('+CMTI', (msg) => {
  console.log('Incoming Message', msg);
});

modem.on('+HTTPACTION', (msg) => {
  console.log('Incoming Message', msg);
});

modem.on('+HTTPREAD', (msg) => {
  console.log('Incoming Message', msg);
});

function wait(timeout) {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve();
    }, timeout);
  });
}


// modem.send(testString);
async function start(tim) {
  await wait(tim);
  // modem.open();
  // await modem.sms_read_all(4).then(res => {
  //   console.log(res);
  // }).catch(err => console.log(err));

  // await modem.sms_simple_send('+8613501070260', 'This is a test from gsm2').then(res => console.log(res)).catch(e => console.log(e));
  // let sc = '';
  // await modem.sms_center().then(res => sc = res).catch(e => console.log(e));
  // console.log(sc);
  // await modem.sms_mode(0);
  
  // await modem.sms_simple_send('+8613501070260', 'aaaaaa').then(res => sc = res).catch(e => console.log('111' + e));
  // await modem.set_gsm();
  // await modem.sms_send('+8613501070260', '工作愉快！', sc).then(res => console.log(res)).catch(e => console.log('111' + e));
  // await modem.set('CMGS',25).then(res => console.log(res)).catch(e => console.log('111' + e));
  // await 
  // await modem.sms_simple_send('+8613501070260', 'aaaaaa').then(res => sc = res).catch(e => console.log('222' + e));
  // for (let i = 0; i < 2; i++) {
  //   let msg = '工作愉快！' + i;
  //   await modem.sms_send('+8613811074203', msg, sc);
  //   // await modem.sms_send('+8613501070260', msg, sc).then(res => sc = res).catch(e => console.log('111' + e));
  //   // await modem.sms_send('+8613219860708', msg, sc).then(res => console.log(res)).catch(e => console.log('111' + e));
  //   // if (i % 2 == 0) {
  //   //   await modem.sms_send('+8613219860708', msg, sc);

  //   // } else {
  //   //   await modem.sms_send('+8613811074203', msg, sc);
  //   // }
  // }
  // let i = 0;
  // let sendi = setInterval(() => {
  //   let msg = '工作愉快！' + i;
  //   await modem.sms_send('+8613219860708', msg, sc);
  //   await i++;
  //   if (i == 2) {
  //     clearInterval(sendi);
  //   }
  // }, 7000);
  // await modem.test('CGATT').then(res => console.log(res)).catch(e => console.log('111' + e));
  // await modem.exec('HTTPINIT').then(res => console.log(res)).catch(e => console.log('111' + e));

  // await modem.set('HTTPPARA',`"CID",1`).then(res => console.log(res)).catch(e => console.log('111' + e));
  // await modem.set('HTTPPARA',`"URL","api.lsong.org"`).then(res => console.log(res)).catch(e => console.log('111' + e));
  // await modem.set('HTTPACTION',0).then(res => console.log(res)).catch(e => console.log('111' + e));

  await modem.test('CGATT').then(res => console.log(res)).catch(e => console.log('111' + e));
  await wait(tim);
  await modem.set('SAPBR',`3,1,"CONTYPE","GPRS"`).then(res => console.log(res)).catch(e => console.log('111' + e));
  await wait(tim);
  await modem.set('SAPBR',`3,1,"APN","UNINET"`).then(res => console.log(res)).catch(e => console.log('111' + e));
  await wait(tim);
  await modem.set('SAPBR',`1,1`).then(res => console.log(res)).catch(e => console.log('111' + e));
  await wait(tim);
  await modem.set('SAPBR',`2,1`).then(res => console.log(res)).catch(e => console.log('111' + e));
  await wait(tim);
  await modem.exec('HTTPINIT').then(res => console.log(res)).catch(e => console.log('111' + e));
  await wait(tim);
  await modem.set('HTTPSSL',1).then(res => console.log(res)).catch(e => console.log('111' + e));
  await wait(tim);
  await modem.set('HTTPPARA',`"CID",1`).then(res => console.log(res)).catch(e => console.log('111' + e));
  await wait(tim);
  await modem.set('HTTPPARA',`"URL","api.lsong.org"`).then(res => console.log(res)).catch(e => console.log('111' + e));
  await wait(tim*3);
  await modem.request().then(res => console.log(res)).catch(e => console.log('111' + e));
  await wait(tim*5);
  await modem.get_request().then(res => console.log(res)).catch(e => console.log('111' + e));
  // await modem.exec('HTTPTERM').then(res => console.log(res)).catch(e => console.log('111' + e));

}

start(2000);

